import mongoose from "mongoose";
import bcryptjs from "bcryptjs";
import { ACTION, registerResouceModel } from ".";

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
    select: false,
    set(val: string) {
      return bcryptjs.hashSync(val, 10);
    },
  },
  phone: {
    type: String,
    required: true,
  },
  nickname: {
    type: String,
    default: "",
  },
});

const User = mongoose.model("User", userSchema);

/** 注册模型，并设置访问权限 */
registerResouceModel({
  name: User.modelName,
  model: User,
  action: ACTION.READ,
  desc: "用户",
});

export default User;
